using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class PlayerLobby : NetworkLobbyPlayer
{
	[SyncVar(hook = "OnMyName")]
	public string playerName = string.Empty;

	[SyncVar]
	public int CharacterIndex;

	[SyncVar]
	public string PlayerID = string.Empty;

	[SyncVar(hook = "OnMyTeam")]
	public byte playerTeam;

	public Button RemoveButton;

	public Text Name;

	public Text Status;

	[HideInInspector]
	public bool IsReady;

	private static int kRpcRpcUpdateCountdown;

	private static int kRpcRpcUpdatePlayerLobby;

	private static int kCmdCmdTeamChange;

	private static int kCmdCmdNameChanged;

	private static int kCmdCmdUpdatePlayerInfo;

	public string NetworkplayerName
	{
		get
		{
			return playerName;
		}
		[param: In]
		set
		{
			ref string fieldValue = ref playerName;
			if (NetworkServer.localClientActive && !base.syncVarHookGuard)
			{
				base.syncVarHookGuard = true;
				OnMyName(value);
				base.syncVarHookGuard = false;
			}
			SetSyncVar(value, ref fieldValue, 1u);
		}
	}

	public int NetworkCharacterIndex
	{
		get
		{
			return CharacterIndex;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref CharacterIndex, 2u);
		}
	}

	public string NetworkPlayerID
	{
		get
		{
			return PlayerID;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref PlayerID, 4u);
		}
	}

	public byte NetworkplayerTeam
	{
		get
		{
			return playerTeam;
		}
		[param: In]
		set
		{
			ref byte fieldValue = ref playerTeam;
			if (NetworkServer.localClientActive && !base.syncVarHookGuard)
			{
				base.syncVarHookGuard = true;
				OnMyTeam(value);
				base.syncVarHookGuard = false;
			}
			SetSyncVar(value, ref fieldValue, 8u);
		}
	}

	public override void OnClientEnterLobby()
	{
		GameObject gameObject = GameObject.Find("LobbyInstance");
		if ((bool)gameObject)
		{
			base.transform.SetParent(gameObject.transform);
		}
		base.OnClientEnterLobby();
		if (UnitZ.gameNetwork != null)
		{
			UnitZ.gameNetwork.OnPlayersNumberModified(1);
		}
		if (GameLobby.instance != null)
		{
			GameLobby.instance.AddPlayer(this);
			GameLobby.instance.DisplayDirectServerWarning(base.isServer && UnitZ.gameNetwork.matchMaker == null);
		}
		if (base.isLocalPlayer)
		{
			SetupLocalPlayer();
		}
		else
		{
			SetupOtherPlayer();
		}
		OnMyName(playerName);
		OnMyTeam(playerTeam);
	}

	public override void OnStartAuthority()
	{
		base.OnStartAuthority();
		SetupLocalPlayer();
	}

	private void SetupOtherPlayer()
	{
		RemoveButton.gameObject.SetActive(base.isServer);
		OnClientReady(base.readyToBegin);
	}

	private void SetupLocalPlayer()
	{
		OnPlayerSetup();
		if (base.isServer)
		{
			SendReadyToBeginMessage();
		}
		if (!UnitZ.gameNetwork.IsDedicatedServer)
		{
			if (GameLobby.instance != null)
			{
				if (GameLobby.instance.playerListContentTransform != null)
				{
					CallCmdUpdatePlayerInfo(UnitZ.gameManager.UserName, UnitZ.characterManager.CharacterIndex, UnitZ.gameManager.UserID);
				}
				GameLobby.instance.currentPlayerLobby = this;
			}
			CallCmdTeamChange();
		}
		else if (base.isServer)
		{
			if (UnitZ.dedicatedManager != null)
			{
				UnityEngine.Debug.Log("adding player lobby");
				UnitZ.dedicatedManager.CurrentPlayerLobby = this;
			}
			CallCmdNameChanged("Server");
		}
		if (UnitZ.gameNetwork != null)
		{
			UnitZ.gameNetwork.OnPlayersNumberModified(0);
		}
	}

	public void OnPlayerSetup()
	{
		if (base.isLocalPlayer)
		{
			int num = 0;
			foreach (PlayerController localPlayer in ClientScene.localPlayers)
			{
				num += ((localPlayer != null && localPlayer.playerControllerId != -1) ? 1 : 0);
			}
			if (RemoveButton != null)
			{
				RemoveButton.interactable = (num > 1);
			}
		}
	}

	public override void OnClientReady(bool readyState)
	{
		IsReady = readyState;
		if (readyState)
		{
			if ((bool)Status)
			{
				Status.text = "Ready";
			}
		}
		else if ((bool)Status)
		{
			Status.text = "Waiting";
		}
	}

	public void OnMyName(string newName)
	{
		NetworkplayerName = newName;
		if ((bool)Name)
		{
			Name.text = newName;
		}
	}

	public void OnMyTeam(byte team)
	{
		NetworkplayerTeam = team;
	}

	public void OnPlayerListChanged(int idx)
	{
		RectTransform component = GetComponent<RectTransform>();
		RectTransform rectTransform = component;
		float num = -idx;
		Vector2 sizeDelta = component.sizeDelta;
		rectTransform.anchoredPosition = new Vector2(0f, num * (sizeDelta.y + 3f));
	}

	public void OnReadyClicked()
	{
		SendReadyToBeginMessage();
	}

	public void OnRemovePlayerClick()
	{
		if (base.isLocalPlayer)
		{
			UnitZ.gameNetwork.LeaveMatch();
		}
		else if (base.isServer)
		{
			UnitZ.gameNetwork.KickPlayer(base.connectionToClient);
		}
	}

	[ClientRpc]
	public void RpcUpdateCountdown(int countdown)
	{
		if (UnitZ.Hud != null)
		{
			UnitZ.Hud.ProcessPopup.SetActive(value: true);
		}
	}

	[ClientRpc]
	public void RpcUpdatePlayerLobby()
	{
		OnPlayerSetup();
	}

	[Command]
	public void CmdTeamChange()
	{
		NetworkplayerTeam = 0;
	}

	[Command]
	public void CmdNameChanged(string name)
	{
		NetworkplayerName = name;
		if ((bool)Name)
		{
			Name.text = name;
		}
	}

	[Command]
	public void CmdUpdatePlayerInfo(string name, int characterIndex, string playerID)
	{
		NetworkplayerName = name;
		if ((bool)Name)
		{
			Name.text = name;
		}
		NetworkCharacterIndex = characterIndex;
		NetworkPlayerID = playerID;
	}

	public void OnDestroy()
	{
		if (GameLobby.instance != null)
		{
			GameLobby.instance.RemovePlayer(this);
		}
		if (UnitZ.gameNetwork != null)
		{
			UnitZ.gameNetwork.OnPlayersNumberModified(-1);
		}
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdTeamChange(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdTeamChange called on client.");
		}
		else
		{
			((PlayerLobby)obj).CmdTeamChange();
		}
	}

	protected static void InvokeCmdCmdNameChanged(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdNameChanged called on client.");
		}
		else
		{
			((PlayerLobby)obj).CmdNameChanged(reader.ReadString());
		}
	}

	protected static void InvokeCmdCmdUpdatePlayerInfo(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdUpdatePlayerInfo called on client.");
		}
		else
		{
			((PlayerLobby)obj).CmdUpdatePlayerInfo(reader.ReadString(), (int)reader.ReadPackedUInt32(), reader.ReadString());
		}
	}

	public void CallCmdTeamChange()
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdTeamChange called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdTeamChange();
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdTeamChange);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendCommandInternal(networkWriter, 0, "CmdTeamChange");
	}

	public void CallCmdNameChanged(string name)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdNameChanged called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdNameChanged(name);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdNameChanged);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(name);
		SendCommandInternal(networkWriter, 0, "CmdNameChanged");
	}

	public void CallCmdUpdatePlayerInfo(string name, int characterIndex, string playerID)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdUpdatePlayerInfo called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdUpdatePlayerInfo(name, characterIndex, playerID);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdUpdatePlayerInfo);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(name);
		networkWriter.WritePackedUInt32((uint)characterIndex);
		networkWriter.Write(playerID);
		SendCommandInternal(networkWriter, 0, "CmdUpdatePlayerInfo");
	}

	protected static void InvokeRpcRpcUpdateCountdown(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcUpdateCountdown called on server.");
		}
		else
		{
			((PlayerLobby)obj).RpcUpdateCountdown((int)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeRpcRpcUpdatePlayerLobby(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("RPC RpcUpdatePlayerLobby called on server.");
		}
		else
		{
			((PlayerLobby)obj).RpcUpdatePlayerLobby();
		}
	}

	public void CallRpcUpdateCountdown(int countdown)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcUpdateCountdown called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcUpdateCountdown);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32((uint)countdown);
		SendRPCInternal(networkWriter, 0, "RpcUpdateCountdown");
	}

	public void CallRpcUpdatePlayerLobby()
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("RPC Function RpcUpdatePlayerLobby called on client.");
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)2);
		networkWriter.WritePackedUInt32((uint)kRpcRpcUpdatePlayerLobby);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		SendRPCInternal(networkWriter, 0, "RpcUpdatePlayerLobby");
	}

	static PlayerLobby()
	{
		kCmdCmdTeamChange = 1536877538;
		NetworkBehaviour.RegisterCommandDelegate(typeof(PlayerLobby), kCmdCmdTeamChange, InvokeCmdCmdTeamChange);
		kCmdCmdNameChanged = 2104193236;
		NetworkBehaviour.RegisterCommandDelegate(typeof(PlayerLobby), kCmdCmdNameChanged, InvokeCmdCmdNameChanged);
		kCmdCmdUpdatePlayerInfo = -1917126515;
		NetworkBehaviour.RegisterCommandDelegate(typeof(PlayerLobby), kCmdCmdUpdatePlayerInfo, InvokeCmdCmdUpdatePlayerInfo);
		kRpcRpcUpdateCountdown = -1975760259;
		NetworkBehaviour.RegisterRpcDelegate(typeof(PlayerLobby), kRpcRpcUpdateCountdown, InvokeRpcRpcUpdateCountdown);
		kRpcRpcUpdatePlayerLobby = 2094925505;
		NetworkBehaviour.RegisterRpcDelegate(typeof(PlayerLobby), kRpcRpcUpdatePlayerLobby, InvokeRpcRpcUpdatePlayerLobby);
		NetworkCRC.RegisterBehaviour("PlayerLobby", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool flag = base.OnSerialize(writer, forceAll);
		if (forceAll)
		{
			writer.Write(playerName);
			writer.WritePackedUInt32((uint)CharacterIndex);
			writer.Write(PlayerID);
			writer.WritePackedUInt32(playerTeam);
			return true;
		}
		bool flag2 = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag2)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag2 = true;
			}
			writer.Write(playerName);
		}
		if ((base.syncVarDirtyBits & 2) != 0)
		{
			if (!flag2)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag2 = true;
			}
			writer.WritePackedUInt32((uint)CharacterIndex);
		}
		if ((base.syncVarDirtyBits & 4) != 0)
		{
			if (!flag2)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag2 = true;
			}
			writer.Write(PlayerID);
		}
		if ((base.syncVarDirtyBits & 8) != 0)
		{
			if (!flag2)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag2 = true;
			}
			writer.WritePackedUInt32(playerTeam);
		}
		if (!flag2)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag2 | flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		base.OnDeserialize(reader, initialState);
		if (initialState)
		{
			playerName = reader.ReadString();
			CharacterIndex = (int)reader.ReadPackedUInt32();
			PlayerID = reader.ReadString();
			playerTeam = (byte)reader.ReadPackedUInt32();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			OnMyName(reader.ReadString());
		}
		if ((num & 2) != 0)
		{
			CharacterIndex = (int)reader.ReadPackedUInt32();
		}
		if ((num & 4) != 0)
		{
			PlayerID = reader.ReadString();
		}
		if ((num & 8) != 0)
		{
			OnMyTeam((byte)reader.ReadPackedUInt32());
		}
	}
}
